package com.dj.mingkong.common.util;

import com.dj.mingkong.common.exception.BizException;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.file.Files;

public final class FileUtils {
    private static final Logger LOG = LoggerFactory.getLogger(FileUtils.class);
    private static final char SPOT = '.';

    private FileUtils() {
    }

    public static byte[] fileToByteArray(File file) {
        byte[] data = null;

        try {
            FileInputStream fi = new FileInputStream(file);
            Throwable var3 = null;

            try {
                ByteArrayOutputStream bo = new ByteArrayOutputStream();
                Throwable var5 = null;

                try {
                    byte[] buffer = new byte[1024];

                    int len;
                    while ((len = fi.read(buffer)) != -1) {
                        bo.write(buffer, 0, len);
                    }

                    data = bo.toByteArray();
                } catch (Throwable var31) {
                    var5 = var31;
                    throw var31;
                } finally {
                    if (bo != null) {
                        if (var5 != null) {
                            try {
                                bo.close();
                            } catch (Throwable var30) {
                                var5.addSuppressed(var30);
                            }
                        } else {
                            bo.close();
                        }
                    }

                }
            } catch (Throwable var33) {
                var3 = var33;
                throw var33;
            } finally {
                if (fi != null) {
                    if (var3 != null) {
                        try {
                            fi.close();
                        } catch (Throwable var29) {
                            var3.addSuppressed(var29);
                        }
                    } else {
                        fi.close();
                    }
                }

            }
        } catch (Exception var35) {
            LOG.error("fileToByteArray exception, {}", var35.getMessage(), var35);
        }

        return data;
    }

    public static File byteArrayToFile(Byte[] fileData, String filePath) {
        File file = getFile(filePath);

        try {
            FileOutputStream fo = new FileOutputStream(file);
            Throwable var4 = null;

            try {
                fo.write(convert(fileData));
            } catch (Throwable var14) {
                var4 = var14;
                throw var14;
            } finally {
                if (fo != null) {
                    if (var4 != null) {
                        try {
                            fo.close();
                        } catch (Throwable var13) {
                            var4.addSuppressed(var13);
                        }
                    } else {
                        fo.close();
                    }
                }

            }
        } catch (Exception var16) {
            LOG.error("byteArrayToFile exception, {}", var16.getMessage(), var16);
        }

        return file;
    }

    public static File byteArrayToFile(byte[] fileData, String filePath) {
        File file = getFile(filePath);

        try {
            FileOutputStream fo = new FileOutputStream(file);
            Throwable var4 = null;

            try {
                fo.write(fileData);
            } catch (Throwable var14) {
                var4 = var14;
                throw var14;
            } finally {
                if (fo != null) {
                    if (var4 != null) {
                        try {
                            fo.close();
                        } catch (Throwable var13) {
                            var4.addSuppressed(var13);
                        }
                    } else {
                        fo.close();
                    }
                }

            }
        } catch (Exception var16) {
            LOG.error("byteArrayToFile exception, {}", var16.getMessage(), var16);
        }

        return file;
    }

    public static File getFile(String filePath) {
        if (StringUtils.isBlank(filePath)) {
            throw new IllegalArgumentException("文件路径不能空");
        } else {
            return new File(FilenameUtils.normalize(filePath));
        }
    }

    private static byte[] convert(Byte[] fileData) {
        byte[] data = new byte[fileData.length];

        for (int i = 0; i < fileData.length; ++i) {
            data[i] = fileData[i];
        }

        return data;
    }

    public static void copyFile(File source, File dest) {
        try {
            InputStream input = new FileInputStream(source);
            Throwable var3 = null;

            try {
                OutputStream output = new FileOutputStream(dest);
                Throwable var5 = null;

                try {
                    write(input, output);
                } catch (Throwable var30) {
                    var5 = var30;
                    throw var30;
                } finally {
                    if (output != null) {
                        if (var5 != null) {
                            try {
                                output.close();
                            } catch (Throwable var29) {
                                var5.addSuppressed(var29);
                            }
                        } else {
                            output.close();
                        }
                    }

                }
            } catch (Throwable var32) {
                var3 = var32;
                throw var32;
            } finally {
                if (input != null) {
                    if (var3 != null) {
                        try {
                            input.close();
                        } catch (Throwable var28) {
                            var3.addSuppressed(var28);
                        }
                    } else {
                        input.close();
                    }
                }

            }
        } catch (Exception var34) {
            LOG.error("copyFile exception, {}", var34.getMessage(), var34);
        }

    }

    private static void write(InputStream input, OutputStream output) throws IOException {
        byte[] buf = new byte[1024];

        int bytesRead;
        while ((bytesRead = input.read(buf)) != -1) {
            output.write(buf, 0, bytesRead);
        }

    }

    public static boolean mkDirs(String path) {
        File file = getFile(path);
        return file.exists() ? file.isDirectory() : file.mkdirs();
    }

    public static String getFileNameSuffix(String fileName) {
        if (StringUtils.isBlank(fileName)) {
            throw new BizException("文件名称不能为空");
        } else {
            int lastIndexOf = fileName.lastIndexOf(46);
            if (lastIndexOf == -1) {
                throw new BizException("文件名称没有后缀");
            } else {
                return fileName.substring(lastIndexOf);
            }
        }
    }

    public static boolean deleteFile(String filePath) {
        File file = getFile(filePath);
        return deleteFile(file);
    }

    public static boolean deleteFile(File file) {
        if (file == null) {
            return false;
        } else if (file.exists()) {
            try {
                Files.delete(file.toPath());
                return true;
            } catch (IOException var2) {
                return false;
            }
        } else {
            return true;
        }
    }

    public static boolean deleteQuietly(File file) {
        return org.apache.commons.io.FileUtils.deleteQuietly(file);
    }

    public static void closeStream(OutputStream os, InputStream is) {
        if (null != os) {
            try {
                os.close();
            } catch (IOException var4) {
                LOG.warn("close OutputStream error, {}", var4.getMessage());
            }
        }

        if (is != null) {
            try {
                is.close();
            } catch (IOException var3) {
                LOG.warn("close InputStream error, {}", var3.getMessage());
            }
        }

    }
}

